package com.zlc.common.core.process.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zlc.common.common.service.BaseServiceImpl;
import com.zlc.common.core.process.mapper.StoneEventConfigMapper;
import com.zlc.common.core.process.model.entity.StoneEventConfig;
import com.zlc.common.core.process.model.vo.StoneEventConfigVo;
import com.zlc.common.core.process.model.vo.StoneProcessAuthConfigVo;
import com.zlc.common.core.process.service.StoneEventConfigService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.stream.Collectors;

import static com.zlc.common.common.constant.WorkflowConstant.TYPE_NODE;
import static com.zlc.common.common.constant.WorkflowConstant.TYPE_PROCESS;

/**
 * @author  zl
 * @date 2023-10-17 
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class StoneEventConfigServiceImpl extends BaseServiceImpl<StoneEventConfigMapper, StoneEventConfig, StoneEventConfigVo> implements StoneEventConfigService{

    @Override
    public int getCountByInterId(String interId) {
        LambdaQueryWrapper<StoneEventConfig> wrapper = wrapper().eq(StoneEventConfig::getInterId, interId);
        return count(wrapper);
    }


    @Override
    public void removeByProcessId(String processId) {
        LambdaQueryWrapper<StoneEventConfig> wrapper = wrapper()
                .eq(StoneEventConfig::getProcessId, processId);
        List<StoneEventConfigVo> nodeVos = queryVoList(wrapper);
        if(!ObjectUtils.isEmpty(nodeVos)){
            List<String> ids = nodeVos.stream().map(StoneEventConfigVo::getId).collect(Collectors.toList());
            deleteObjByIds(ids);
        }
    }

    @Override
    public List<StoneEventConfigVo> getProcessEvents(String processId) {
        LambdaQueryWrapper<StoneEventConfig> wrapper = new LambdaQueryWrapper<StoneEventConfig>()
                .eq(StoneEventConfig::getProcessId, processId)
                .eq(StoneEventConfig::getType, TYPE_PROCESS)
                .orderByAsc(StoneEventConfig::getOrder);
        return queryVoList(wrapper);
    }

    @Override
    public List<StoneEventConfigVo> getByProcessId(String processId) {
        LambdaQueryWrapper<StoneEventConfig> wrapper = new LambdaQueryWrapper<StoneEventConfig>()
                .eq(StoneEventConfig::getProcessId, processId)
                .orderByAsc(StoneEventConfig::getOrder);
        return queryVoList(wrapper);
    }

    @Override
    public List<StoneEventConfigVo> getNodeEvents(String processId, String nodeId) {

        LambdaQueryWrapper<StoneEventConfig> wrapper = new LambdaQueryWrapper<StoneEventConfig>()
                .eq(StoneEventConfig::getProcessId, processId)
                .eq(StoneEventConfig::getNodeId,nodeId)
                .eq(StoneEventConfig::getType, TYPE_NODE)
                .orderByAsc(StoneEventConfig::getOrder);
        return queryVoList(wrapper);
    }
}
